﻿using System;
using System.Collections.Generic;
using System.Text;
using SlamDunk.CMS.Common;

namespace SlamDunk.CMS.Model
{
    /// <summary>
    /// Article实体类（手写部分）
    /// </summary>
    public partial class Article
    {
        #region 01.方法——转换为视图模型对象（展示文章）
        /// <summary>
        /// 转换为视图模型对象方法（展示文章）
        /// </summary>
        /// <returns>视图模型对象</returns>
        public Model.ArticleViewForRead ToViewModel()
        {
            return new Model.ArticleViewForRead()
            {
                Id = this.Id,
                Channel = this.Channel,
                Title = FormatTitle(this.Title),
                Intitle = this.Intitle,
                SubTitle = this.SubTitle,
                Author = this.Author,
                Source = this.Source,
                Content = FormatContent(this.Content),
                Sort = this.Sort,
                Publish = Convert.ToBoolean(this.Publish) ? "已发" : "未发",
                IsTop = Convert.ToBoolean(this.IsTop) ? "是" : "否",
                Click = this.Click,
                ImagePath = this.ImagePath,
                ThumbPath = this.ThumbPath,
                Attachment = this.Attachment,
                AddTime = this.AddTime.ToString("yyyy-MM-dd"),
                Url = this.GetUrl()
            };
        }
        #endregion

        #region 02.方法——转换为视图模型对象（编辑文章）
        /// <summary>
        /// 转换为视图模型对象方法（编辑文章）
        /// </summary>
        /// <returns>视图模型对象</returns>
        public Model.ArticleViewForUpdate ToUpdateModel()
        {
            return new Model.ArticleViewForUpdate()
            {
                Id = this.Id,
                UserId = this.UserId,
                Title = this.Title,
                Intitle = this.Intitle,
                SubTitle = this.SubTitle,
                Author = this.Author,
                Source = this.Source,
                Content = this.Content,
                IsTop = this.IsTop,
                ImagePath = this.ImagePath,
                ThumbPath = this.ThumbPath,
                Attachment = this.Attachment
            };
        }
        #endregion

        #region 02.方法——获取文章Url地址
        /// <summary>
        /// 获取文章Url地址
        /// </summary>
        /// <param name="model">文章实体对象</param>
        /// <returns>Url地址</returns>
        public string GetUrl()
        {
            if (this.IsLink == null || this.IsLink == false)
            {
                return string.Format("{0}{1}{2}{3}", this.Channel.SaveDirectory, this.SaveDirectory, this.FileName, this.FileExtName);
            }
            else
            {
                return this.Content;
            }
        }
        #endregion

        #region 03.方法——格式化文章标题方法
        /// <summary>
        /// 方法——格式化文章标题方法
        /// </summary>
        /// <param name="title">文章标题</param>
        /// <returns>格式化后的文章标题</returns>
        private string FormatTitle(string title)
        {
            title = title.NoHtml();     //去除HTML标签
            if (title.Length > 22)
            {
                title = string.Format("{0}...", title.Substring(0, 21));
                return title;
            }
            else
            {
                return title;
            }
        }
        #endregion

        #region 04.方法——格式化文章内容方法
        /// <summary>
        /// 方法——格式化文章内容方法
        /// </summary>
        /// <param name="contents">文章内容</param>
        /// <returns>文章内容</returns>
        private string FormatContent(string content)
        {
            content = content.NoHtml();     //去除HTML标签
            if (content.Length > 95)
            {
                content = string.Format("{0}...", content.Substring(0, 94));
                return content;
            }
            else
            {
                return content;
            }
        }
        #endregion

        #region 05.方法——获取文章链接字符串
        /// <summary>
        /// 获取文章链接字符串
        /// </summary>
        /// <returns>文章链接字符串</returns>
        public string GetLinkString()
        {
            string link = "<a href='{0}'>{1}</a>";
            return string.Format(link, this.GetUrl(), this.Title.NoHtml());
        }
        #endregion
    }
}